package co.yixiang.yshop.module.tables.service.reservation;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import co.yixiang.yshop.module.tables.controller.admin.reservation.vo.*;
import co.yixiang.yshop.module.tables.dal.dataobject.reservation.ReservationDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.tables.convert.reservation.ReservationConvert;
import co.yixiang.yshop.module.tables.dal.mysql.reservation.ReservationMapper;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.tables.enums.ErrorCodeConstants.*;

/**
 * 预约记录 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class ReservationServiceImpl implements ReservationService {

    @Resource
    private ReservationMapper reservationMapper;

    @Override
    public Integer createReservation(ReservationCreateReqVO createReqVO) {
        // 插入
        ReservationDO reservation = ReservationConvert.INSTANCE.convert(createReqVO);
        reservationMapper.insert(reservation);
        // 返回
        return reservation.getId();
    }

    @Override
    public void updateReservation(ReservationUpdateReqVO updateReqVO) {
        // 校验存在
        validateReservationExists(updateReqVO.getId());
        // 更新
        ReservationDO updateObj = ReservationConvert.INSTANCE.convert(updateReqVO);
        reservationMapper.updateById(updateObj);
    }

    @Override
    public void deleteReservation(Integer id) {
        // 校验存在
        validateReservationExists(id);
        // 删除
        reservationMapper.deleteById(id);
    }

    private void validateReservationExists(Integer id) {
        if (reservationMapper.selectById(id) == null) {
            throw exception(RESERVATION_NOT_EXISTS);
        }
    }

    @Override
    public ReservationDO getReservation(Integer id) {
        return reservationMapper.selectById(id);
    }

    @Override
    public List<ReservationDO> getReservationList(Collection<Integer> ids) {
        return reservationMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ReservationDO> getReservationPage(ReservationPageReqVO pageReqVO) {
        return reservationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ReservationDO> getReservationList(ReservationExportReqVO exportReqVO) {
        return reservationMapper.selectList(exportReqVO);
    }

}
